package com.rk.kbk.controller;

/**
 * Created by bilibili on 2017/3/13.
 */


import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.FeedBackDao;
import com.rk.kbk.dao.UserDao;
import com.rk.kbk.dto.PageDto;
import com.rk.kbk.model.FeedBack;
import com.rk.kbk.model.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 * @CashRecordController
 * @用户商家现金流水Controller
 * @version : Ver 1.0
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/feedback")
public class FeedBackController {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private FeedBackDao feedBackDao;
    @Autowired
    private UserDao userDao;

    /**
     * 意见反馈保存
     */
    @RequestMapping(value = "/saveFeedBackMessage",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp saveFeedBackMessage(@RequestBody FeedBack feedBack){
        log.debug("意见反馈保存");
        User user=userDao.findOne(feedBack.getUser().getId());
        feedBack.setUser(user);
        feedBackDao.save(feedBack);
        return JsonResp.ok();
    }

    /**
     * @删除投诉-one(永久)
     * @param id 投诉id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteFeedBack",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteAddressOne(Long id) {
        log.debug("删除地址-one(永久)");
        feedBackDao.delete(id);
        return JsonResp.ok();
    }

    /**
     * 分页查询(jpa方式)
     * @param feedbackType 投诉类型
     * @param pageNum 第几页
     * @param pageSize 每页的条数
     * @return JsonResp
     */
    @RequestMapping(value = "/jpa/page",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPageFeedBackJpa( Integer pageNum,Integer pageSize,Integer feedbackType) {
        log.debug("分页查询(jpa方式)");
        //多条件排序Sort
        //Sort sort = new Sort(Sort.Direction.DESC, "createdate").and(new Sort(Sort.Direction.AES, "id"));
        //排序Sort
        Sort sort = new Sort(Sort.Direction.DESC, "gmtDatetime");
        Pageable pageable = new PageRequest(pageNum-1, pageSize, sort);
        Page page;
        if (feedbackType==0) {
            page = feedBackDao.findAll(pageable); //无条件分页
        }else{
            page = feedBackDao.findAll( 		  //条件分页
                    (root, criteriaQuery, cb) -> {
                        Predicate predicate = cb.conjunction();
                        List<Expression<Boolean>> expressions = predicate.getExpressions();
                            expressions.add(cb.equal(root.get("feedbackType"), feedbackType));
                        return predicate;
                    }, pageable);
        }
        return JsonResp.ok(new PageDto(page));
    }
    /**
     * 投诉详情
     */
    @RequestMapping(value = "/feedBackDetails",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp FeedBackDetails(Long id){
        log.debug("投诉详情");
        return JsonResp.ok(feedBackDao.findOne(id));
    }






}
